package com.example.easypad;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class activity_register extends AppCompatActivity {
    private EditText et_reUseName;
    private EditText et_rePassWord;
    private EditText et_rePassWordAgain;
    private Button btn_reLogin;
    private TextView tv_gotoLogin;
    public static SQLiteOpenHelper helper = null;
    public static SQLiteDatabase db;
    private Cursor cursor = null;
    private boolean isExist = false;
    String userName,passWord,passWordAgain,sql = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        MakeStatusBarTransparent.makeStatusBarTransparent(this);
        initLayout();
        helper = new ContactSQLiteOpenHelper(this,"demo.db",null,2);
        db = helper.getWritableDatabase();
        btn_reLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    register();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        tv_gotoLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), activity_login.class);
                startActivity(intent);
            }
        });
    }

    private void register() throws InterruptedException {
        userName = et_reUseName.getText() + "";
        passWord = et_rePassWord.getText() + "";
        passWordAgain = et_rePassWordAgain.getText() + "";
        if (userName.equals("") || passWord.equals("") || passWordAgain.equals("")) {
            Toast.makeText(getApplicationContext(), "请完善注册信息", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!passWord.equals(passWordAgain)) {
            Toast.makeText(getApplicationContext(), "两次密码输入不相同", Toast.LENGTH_SHORT).show();
            return;
        }
        String sql_qurey = "select * from User where username =?";
        String [] args = new String[]{userName};
        queryUserForEx(sql_qurey,args);
        if(!isExist){
            sql = "insert into user (username,password) values ('" + userName + "','" + passWord + "')";
            db.execSQL(sql);
            Toast.makeText(this, "注册成功,即将跳转登录页面", Toast.LENGTH_SHORT).show();
            Thread.sleep(2000);
            Intent intent = new Intent(getApplicationContext(), activity_login.class);
            startActivity(intent);
        }else{
            Toast.makeText(this, "用户名已存在！", Toast.LENGTH_SHORT).show();
        }

    }
    private void queryUserForEx(String sql,String[] selection){
        cursor = db.rawQuery(sql, selection);
        if(cursor.getCount() != 0)
            isExist = true;
        else
            isExist = false;

    }
    private void initLayout() {
        et_reUseName = findViewById(R.id.et_reUseName);
        et_rePassWord = findViewById(R.id.et_rePassWord);
        et_rePassWordAgain = findViewById(R.id.et_rePassWordAgain);
        btn_reLogin = findViewById(R.id.btn_reLogin);
        tv_gotoLogin = findViewById(R.id.tv_gotoLogin);
    }
}